<?php
declare (strict_types=1);

namespace app\command;

use app\library\websocket\event\CloseEvent;
use app\library\websocket\event\ErrorEvent;
use app\library\websocket\event\MessageEvent;
use app\library\websocket\event\OpenEvent;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

class Websocket extends Command
{
	protected function configure()
	{
		// 指令配置
		$this->setName('websocket')
			->addArgument('host', Argument::OPTIONAL, '监听的地址', config('websocket.host'))
			->addArgument('port', Argument::OPTIONAL, '监听的端口', config('websocket.port'))
			->setDescription('初始化websocket链接');
	}

	protected function execute(Input $input, Output $output)
	{
		$host = $input->getArgument('host');
		$port = $input->getArgument('port');
		$server = new \app\library\websocket\Websocket($host, $port);
		$server->on('open', [OpenEvent::class, 'handle']);
		$server->on('message', [MessageEvent::class, 'handle']);
		$server->on('close', [CloseEvent::class, 'handle']);
//		$server->on('workerError', [ErrorEvent::class, 'handle']);
		$server->set(config('websocket.setting'));
		$server->run();
	}
}
